package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.AdministratorTableBiz;
import com.tszs.biz.MainAuthorityBiz;
import com.tszs.biz.PersonalAuthorityBiz;
import com.tszs.biz.SubJurisdictionBiz;
import com.tszs.entity.AdministratorTable;
import com.tszs.entity.AdministratorTableExample;
import com.tszs.entity.AdministratorTableExample.Criteria;
import com.tszs.entity.MainAuthority;
import com.tszs.entity.MainAuthorityExample;
import com.tszs.entity.PersonalAuthority;
import com.tszs.entity.PersonalAuthorityExample;
import com.tszs.entity.SubJurisdiction;
import com.tszs.entity.SubJurisdictionExample;
import com.tszs.util.CallBack;
import com.tszs.util.MD5keyBean;
import com.tszs.util.Uuid;

import net.sf.json.JSONObject;

/**
 * @类名:AdministratorTableController
 * @类描述：管理员表操作 @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class AdministratorTableController {
	/**
	 * @AdministratorTableBiz
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private AdministratorTableBiz administratorTableBiz;
	/**
	 * @MainAuthorityBiz
	 * @描述:主菜单
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private MainAuthorityBiz mainAuthorityBiz;

	/**
	 * @SubJurisdictionBiz
	 * @描述:子菜单
	 * @创建时间:2019-03-21 13:14:14
	 */
	@Autowired
	private SubJurisdictionBiz subJurisdictionBiz;
	/**
	 * PersonalAuthorityBiz
	 * 
	 * @描述: 个人权限表添加
	 * @创建时间:2019-03-21 13:14:14
	 */
	@Autowired
	private PersonalAuthorityBiz personalAuthorityBiz;

	/**
	 * @方法名:administratorAdds
	 * @方法描述：管理员查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/administratorAdds")
	public void administratorAdds(HttpServletResponse response, HttpServletRequest request) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		AdministratorTable example = new AdministratorTable();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		List<AdministratorTable> selectByExample = administratorTableBiz.selectByExamplePage(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("id", i + 1);
			mapData.put("aUsername", selectByExample.get(i).getaUsername());
			mapData.put("CreateTime", selectByExample.get(i).getaCreateTime());
			String aId = selectByExample.get(i).getaId();
			mapData.put("button",
					"<a href=\"javascript:table('0','" + aId + "')\"  class=\"layui-btn layui-btn-normal layui-btn-xs\" >查看权限</a> "
				+ "<a href=\"javascript:table('1','" + aId + "')\" class=\"layui-btn layui-btn-warm layui-btn-xs\" style=''>修改权限</a> "
				+ "<a href=\"javascript:resetPwd('" + aId + "')\" class='layui-btn layui-btn-xs layui-btn-danger'>重置密码</a>"
				+ "<a href=\"javascript:table('2','" + aId + "')\" class=\"layui-btn layui-btn-xs\">删除</a>");

			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @方法名:menu
	 * @方法描述：管理员添加所需的权限主权限查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/menu")
	public void menu(HttpServletResponse response) {
		// 主权限查询
		MainAuthorityExample mainAuthorityExample = new MainAuthorityExample();
		com.tszs.entity.MainAuthorityExample.Criteria mainAuthorityca = mainAuthorityExample.createCriteria();
		mainAuthorityca.andMZstaticEqualTo("0");
		List<MainAuthority> mainAuthority = mainAuthorityBiz.selectByExample(mainAuthorityExample);
		new CallBack().yes(response, mainAuthority, "success");
	}

	/**
	 * @方法名:sub
	 * @方法描述：管理员添加所需的权限子查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/sub")
	public void Sub(HttpServletResponse response) {
		// 子查询
		SubJurisdictionExample subJurisdictionExample = new SubJurisdictionExample();
		com.tszs.entity.SubJurisdictionExample.Criteria subJurisdictionca = subJurisdictionExample.createCriteria();
		subJurisdictionca.andSStaticEqualTo("0");
		List<SubJurisdiction> subJurisdiction = subJurisdictionBiz.selectByExample(subJurisdictionExample);
		new CallBack().yes(response, subJurisdiction, "success");
	}
	
	/*	*//**
	 * @throws IOException 
	 * @throws ServletException 
	 * @方法名:findAllMenSub
	 * @方法描述：管理员添加所需的权限子查询  Ztree树展示
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *		{ id:1, pId:0, name:"随意勾选 1", open:true},
			{ id:111, pId:1, name:"随意勾选 1-1-1"},
			{ id:112, pId:1, name:"随意勾选 1-1-2"},
			{ id:121, pId:1, name:"随意勾选 1-2-1"},
			{ id:122, pId:1, name:"随意勾选 1-2-2"},
			{ id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
			{ id:21, pId:2, name:"随意勾选 2-1"},
			{ id:23, pId:2, name:"随意勾选 2-3"}
	 *//*
	@RequestMapping("pc/admin/findAllMenSub")
	public void findAllMenSub(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		List<MainAuthority> menu=menu();
		List<SubJurisdiction> sub=Sub(); 
		List<Map<String, Object>> list=new ArrayList<>();
		for (int i = 0; i < menu.size(); i++) {
			Map<String, Object>map=new HashMap<>();
			map.put("id", menu.get(i).getmId());
			map.put("pId","0");
			map.put("name",menu.get(i).getmNames());
			map.put("open", true);
			list.add(map);
			String mId=menu.get(i).getmId();
			for (int j = 0; j < sub.size(); j++) {
				if(sub.get(j).getmId().equals(mId)) {
					Map<String, Object>map2=new HashMap<>();
					map2.put("id", sub.get(j).getsId());
					map2.put("pId",sub.get(j).getmId());
					map2.put("name",sub.get(j).getsPathName());
					list.add(map2);
				}
			}
		}
		new CallBack().yes(response, list, "");
	}*/
	
	
	/**
	 * @方法名:addedMembershipPrivileges
	 * @方法描述：管理员添加 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/addedMembershipPrivileges")
	public void addedMembershipPrivileges(HttpServletRequest request, HttpServletResponse response) {
		String createTime = Uuid.getTime();
		String id = Uuid.getUUID();
		AdministratorTable example = new AdministratorTable();
		example.setaUsername(request.getParameter("aUsername"));
		List<AdministratorTable> selectByinsert = administratorTableBiz.selectByinsert(example);
		if (selectByinsert.size() > 0) {
			new CallBack().no(response, "", "添加失败账号已经存在");
		} else {
			// 1. 账号密码表
			AdministratorTable record = new AdministratorTable();
			// UUID
			record.setaId(id);
			// 密码MD5
			MD5keyBean m = new MD5keyBean();
			String md5 = m.getkeyBeanofStr(request.getParameter("aPassword"));
			record.setaPassword(md5);
			// 状态0正常
			record.setaStatic("0");
			// 账号
			record.setaUsername(request.getParameter("aUsername"));
			// 创建时间
			record.setaCreateTime(createTime);
			administratorTableBiz.insertSelective(record);
			// 2.个人权限表
			PersonalAuthority record1 = new PersonalAuthority();
			record1.setpCreateTime(createTime);
			record1.setpId(id);
			record1.setpMainId(request.getParameter("pMainId"));
			record1.setpSubId(request.getParameter("pSubId"));
			record1.setpStatic("0");
			personalAuthorityBiz.insertSelective(record1);
			new CallBack().yes(response, "", "添加成功");
		}

	}
	/**
	 * 以逗号分隔装入list集合然后 mybaits foreach查询
	 * @param names
	 */
	public List<String> splitstring(String names) {
		List<String>list=new ArrayList<>();
		for (int i = 0; i < names.split(",").length; i++) {
			list.add(names.split(",")[i]);
		}
		return list;
	}
	/**
	 * 查询个人权限
	 * @param id
	 * @return
	 */
	public List<PersonalAuthority> selectByExample(String id){
		PersonalAuthorityExample example=new PersonalAuthorityExample();
		com.tszs.entity.PersonalAuthorityExample.Criteria ca=example.createCriteria();
		ca.andPIdEqualTo(id);
		return personalAuthorityBiz.selectByExample(example);
	}
	/**
	 * @方法名:viewPermissionfindMen
	 * @方法描述：个人主权限  查询
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/viewPermissionfindMen")
	public void viewPermissionfindMen(HttpServletRequest request,HttpServletResponse response){
		List<PersonalAuthority> selectByExample=selectByExample(request.getParameter("id"));
		if(!selectByExample.isEmpty()) {
			List<String> splitstring=splitstring(selectByExample.get(0).getpMainId());
			//2.主权限查询
			MainAuthorityExample mainAuthorityExample = new MainAuthorityExample();
			com.tszs.entity.MainAuthorityExample.Criteria mainAuthorityca = mainAuthorityExample.createCriteria();
			mainAuthorityca.andMIdIn(splitstring);
			List<MainAuthority> mainAuthority = mainAuthorityBiz.selectByExample(mainAuthorityExample);
			new CallBack().yes(response, mainAuthority, "success");
		}
	}
	
	/**
	 * @方法名:viewPermissionfindMen
	 * @方法描述：个人子权限 查询
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/viewPermissionfindSub")
	public void  viewPermissionfindSub(HttpServletRequest request,HttpServletResponse response){
		List<PersonalAuthority> selectByExample=selectByExample(request.getParameter("id"));
		if(!selectByExample.isEmpty()) {
			List<String> splitstring=splitstring(selectByExample.get(0).getpSubId());
			//3.子查询
			SubJurisdictionExample subJurisdictionExample = new SubJurisdictionExample();
			com.tszs.entity.SubJurisdictionExample.Criteria subJurisdictionca = subJurisdictionExample.createCriteria();
			subJurisdictionca.andSIdIn(splitstring);
			List<SubJurisdiction> subJurisdiction = subJurisdictionBiz.selectByExample(subJurisdictionExample);
			new CallBack().yes(response, subJurisdiction, "success");
		}
	}
	/**
	 * @方法名:viewPermissionfindMen
	 * @方法描述：个人子权限 查询
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/changePasswords")
	public void  changePasswords(HttpServletRequest request,HttpServletResponse response){
		String id=request.getParameter("id");
		AdministratorTable record=new AdministratorTable();
		MD5keyBean m = new MD5keyBean();
		String md5 = m.getkeyBeanofStr(request.getParameter("aPassword"));
		record.setaPassword(md5);
		if(id.equals("")||id.equals("null")) {
			AdministratorTableExample example=new AdministratorTableExample();
			Criteria ca=example.createCriteria();
			ca.andAIdEqualTo(request.getSession().getAttribute("aId")+"");
			administratorTableBiz.updateByExampleSelective(record, example);
			new CallBack().yes(response, "", "修改成功");
		}else {
				AdministratorTableExample example=new AdministratorTableExample();
				Criteria ca=example.createCriteria();
				ca.andAIdEqualTo(id);
				administratorTableBiz.updateByExampleSelective(record, example);
				new CallBack().yes(response, "", "修改成功");
		}
	}
	/**
	 * @方法名:deleteAdministrator
	 * @方法描述：管理员删除
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/deleteAdministrator")
	public void deleteAdministrator(HttpServletRequest request,HttpServletResponse response) {
		AdministratorTableExample example=new AdministratorTableExample();
		Criteria ca=example.createCriteria();
		ca.andAIdEqualTo(request.getParameter("id"));
		int i=administratorTableBiz.deleteByExample(example);
		if(i>0) {
			new CallBack().yes(response, "","");
		}else {
			new CallBack().no(response, "","操作失败");
		}
	}
	
	
}
